#include "stdafx.h"
#include <thread>
#include "DKLog.h"
#include <mutex>

using namespace std;

static mutex s_mutex;

#ifdef DK_DEBUG
void dk_log_fun(CString str) {
	static CString strBuf[40];
	static int index = 0;
	static int showIndex = 0;

	strBuf[index++] = str;
	if (index >= 40) {
		index = 0;
	}
	auto mylamthread = [] {
		s_mutex.lock();
		kMainDlg->Log(strBuf[showIndex++]);
		if (showIndex >= 40) {
			showIndex = 0;
		}
		s_mutex.unlock();
	};
	thread mythread3(mylamthread);
	mythread3.detach();
}
#endif

DKLog::DKLog() {

}

DKLog::~DKLog() {

}

void DKLog::dk_log_ln(CString str, ...) {
#ifdef DK_DEBUG
	ATLASSERT(AtlIsValidString(str));
	CString strBuf;

	va_list argList;
	va_start(argList, str);
	strBuf.FormatV(str, argList);
	va_end(argList);

	dk_log_fun(strBuf);
#endif
}

void DKLog::dk_log_hex(CString str, uint8_t* data, uint32_t len) {
#ifdef DK_DEBUG
	CString strBuf = str;
	for (int i = 0; i < len; i++) {
		CString strTem;
		strTem.Format(_T("%02x"), data[i]);
		strBuf = strBuf + strTem;
	}

	dk_log_fun(strBuf);
#endif
}